$(function () {
    lazyCourseImage();
    lazyStudentImage();
    validationStatus();
    QRCodeInp();
    var courseId = $('#courseId').val();
    $("body").on('click', '.a-s-c-d', function () {//添加购物车
        var shopId = $(this).data('s-id');
        var shopType = $(this).data('type');
        addShopCart(shopId, shopType, 1);
    }).on('click', '#pay-course', function () {//立即报名
        var type = $(this).find('span').data('type');
        if (type == 1) {//购买
            var shareKey = getParameter("shareKey");
            if (shareKey != null && $.trim(shareKey) != '') {
                window.location.href = baseloaction + '/web/uc/order/initPayOrder?orderType=COMMON&shopData=' + courseId + '-COURSE-1-' + shareKey;
            } else {
                window.location.href = baseloaction + '/web/uc/order/initPayOrder?orderType=COMMON&shopData=' + courseId + '-COURSE-1';
            }
        }
        if (type == 2) {// 开通会员
            window.location.href = baseloaction + '/web/uc/member/queryMemberList';
        }
    }).on('click', 'h4[data-tab]', function () { //详情/目录/评论切换
        toggleCourseTag($(this));
    }).on('click', '.play-catalog-box,.join,.playback,.end', function () {//点击播放
        sysCheckFn(this, toPlayCourse, 1, 0);

        // openPlayCourse(this,toPlayCourse)
    }).on('click', '#favorites-course', function () {//收藏，取消收藏课程
        var _this = $(this);
        //收藏课程
        saveCourseFavorites(courseId, function (data) {
            var status = data.status;
            if (status == 'add') {//添加收藏
                _this.addClass('c-primary').removeClass('c-999');
                _this.attr('title', '已收藏');
                _this.html('<i class="fa fa-star-o mr5"></i>已收藏');
            } else {//取消收藏
                _this.removeClass('c-primary').addClass('c-999');
                _this.attr('title', '收藏');
                _this.html('<i class="fa fa-star-o mr5"></i>收藏');
            }
        });
    }).on('click', '.play-btn', function () {//点击播放 立即学习
        var buyCourse = $('#buyCourse').val();
        if (buyCourse == "false") {
            guideBuy(courseId, "还未购买此课程，建议立即购买此课，学习更多章节！");
        } else {
            sysCheckFn(this, goPlayCourse, 1, 0);
            // openPlayCourse(this,goPlayCourse);
        }
    }).on('click', '.free-play-btn', function () {
        var freePayType = $(this).data('pay-type');
        if (freePayType == "FREE") {
            genFreeOrder("FREE", courseId + '-COURSE-1', function () {

            });
            $(this).removeClass('free-play-btn free-bm-box');
            $(this).addClass('play-btn');
            $(this).children('span').html('立即学习');
            $('#buyCourse').val('true');
        }
    }).on('click', '#online-consultation', function () {
        var href = $(this).data('href');
        window.open(href, 'newwindow', 'toolbar=no,scro.llbars=yes,location=no,resizable=no,top=200,left=300,width=800,height=600')
    }).on('click', '#show-receive-btn', function () {//点击领取显示
        var courseId = $("#courseId").val();
        getCourseCouponList(courseId)
    }).on('click', '#sFree-play-btn', function () {//免费试看
       var code= $(this).data("code");

       if(code==''||code==null){
           icModale(0,'','暂无试听');
       }else {
           layer.open({
               type: 1,
               title: '免费试看，一睹授课风采',
               area: ['640px', '400px'], //宽高
               content: '<script src="https://v.96koo.net/static/video/js/creatVideoPlayer.js?width=99%&height=99%&videoId=' + code + '"></script>'
           });
       }

    });


    /**
     * 课程详情 目录 评论切换
     */
    function toggleCourseTag(obj) {
        obj.addClass('active').siblings().removeClass('active');
        var tag = obj.data('tab');
        $('article[data-tab=' + tag + ']').removeClass('hide').siblings().addClass('hide');
        if (tag == 'comments') {
            listComment(1); // 课程评价
        }
    }

    //获取购买过课程的用户列表
    findCourseBuyUserList(1, 'carousel-class-member', courseId);

    /**
     * 扫一扫按钮转换链接为二维码
     *
     * @constructor
     */
    function QRCodeInp() {
        var mobilePath = $('#mobilePath').val();
        var courseId = $('#courseId').val();
        var link = mobilePath + '/mobile/course/smallClassInfo?courseId=' + courseId + '&isFromCode';
        var qrcode = $('#QRCodeInp').qrcode({
            render: "canvas",
            height: 140,
            width: 140,
            correctLevel: 2,
            text: link
        }).find('canvas').hide();
        var str = '<img src=' + qrcode[0].toDataURL() + ' width=140 height=140 style=width:140px;height:140px; />';
        $("#QRCodeInp").attr('data-content', str)
    }

    //加载课程可以使用的活动
    setTimeout(function () {
        var price = $("#price").val();
        //获取活动
        courseInfoActivity(price);
    }, 1000);

});